package com.example.customview.silder

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.example.customview.R


open class RecycleAdapter(
    private val brId: Int,
    private val brIndex: Int = 0,
    private val mItemClickListener: ((view: View, item: Any) -> Unit)? = null,
    private val mItemLongClickListener: ((view: View, item: Any) -> Boolean)? = null,
    private val viewHolder: ((viewBinding: ViewDataBinding, item:Any) -> Unit)? = null
) : RecyclerView.Adapter<RecycleAdapter.ViewHolder>() {

    val data: MutableList<Any> = arrayListOf()
    private val mItemLayouts = hashMapOf<String, Int>()
    private val mItemTypes = hashMapOf<Int, Int>()
    private val mItemTypeLayouts = hashMapOf<Int, Int>()

    @LayoutRes
    protected open fun getItemLayout(position: Int): Int {
        val key = data[position].javaClass.name
        return mItemLayouts[key] ?: return android.R.layout.activity_list_item
    }

    fun <T> registerItem(@LayoutRes itemLayout: Int, clazz: Class<T>) {
        mItemTypes[itemLayout] = mItemLayouts.size
        mItemTypeLayouts[mItemLayouts.size] = itemLayout
        mItemLayouts[clazz.name] = itemLayout
    }

    private val mOnClickListener = View.OnClickListener { v ->
        @Suppress("UNCHECKED_CAST")
        mItemClickListener?.invoke(v, v.getTag(R.id.tag_more_key))
    }
    private val mOnLongClickListener = View.OnLongClickListener { v ->
        if (mItemLongClickListener == null)
            return@OnLongClickListener false
        else {
            @Suppress("UNCHECKED_CAST")
            return@OnLongClickListener mItemLongClickListener.invoke(v, v.getTag(R.id.tag_more_key))
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding =
            DataBindingUtil.inflate<ViewDataBinding>(
                LayoutInflater.from(parent.context),
                mItemTypeLayouts[viewType] ?: android.R.layout.activity_list_item,
                parent,
                false
            )
        (parent as RecyclerView).recycledViewPool.setMaxRecycledViews(viewType, 20)
        return ViewHolder(
            binding.root
        )
    }

    override fun getItemViewType(position: Int): Int = mItemTypes[getItemLayout(position)] ?: 0

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val itemView = DataBindingUtil.findBinding<ViewDataBinding>(holder.itemView)
        itemView?.setVariable(brId, data[position])
        if (brIndex != 0) {
            itemView?.setVariable(brIndex, position)
        }
        viewHolder?.invoke(itemView!!, data[position])
        itemView?.root?.apply {
            setTag(R.id.tag_more_key, data[position])
            if (mItemClickListener != null)
                setOnClickListener(mOnClickListener)
            if (mItemLongClickListener != null)
                setOnLongClickListener(mOnLongClickListener)
        }
    }


    override fun getItemCount(): Int = data.size

    class ViewHolder(mView: View) : RecyclerView.ViewHolder(mView)
}